package interfaz;

import java.io.*; 
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import mundo.*;

public class RoboMindCanvas extends Canvas {
   public static int width;
   public static int height;
   public static final int TL_ANCHOR = Graphics.TOP | Graphics.LEFT;
   public static final int TH_ANCHOR = Graphics.TOP | Graphics.HCENTER;
   public static Graphics GRAPHICS;
   public static final int BACKGROUND = 0x808080;
   public Font fontSmall;
   public Font fontMedium;
   public Font fontLarge;
   public Font font;
   public int fontHeight;
   public Image welcomeImage;
   public Image wallImage;
   public Image landImage;
   public Image waterImage;
   public Image plantImage;
   public Image robotImage;
   private MIDlet midlet;
   public Form optionsForm;
   public ChoiceGroup mapChoice;
   public ChoiceGroup scriptChoice;
   public static Image image;
   public Engine engine;
   public RobotAndMap juego;
   public Map mapa;
   public MapPiece[][] place;
   
   public RoboMindCanvas(MIDlet m) {

	   this.midlet = m;
	   width = getWidth();
       height = getHeight();
       System.out.println(width);
       System.out.println(height);
              
   }

   public void initialize() {
	   fontSmall = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
	   fontMedium = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
	   fontLarge = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
       if (height < 180) {
           font = fontSmall;
       } else if (height >= 180 && height < 280) {
           font = fontMedium;
       } else {
           font = fontLarge;
       }       
       fontHeight = font.getHeight();

       try{
		   welcomeImage= Image.createImage("/images/RoboMind.png");		
		   wallImage= Image.createImage("/images/wallImage.png");	
		   landImage= Image.createImage("/images/landImage.png");	
		   waterImage= Image.createImage("/images/waterImage.png");	
		   plantImage= Image.createImage("/images/plantImage.png");	
		   robotImage= Image.createImage("/images/robotImage.png");	
		   
	   }
	   catch(Exception e){System.out.println("Error in board images");}
	   try{
		   mapa = new Map(	"BBBBBBBBBB\n" +
    	   					"BLLBLLLLLB\n" +
    	   					"BLLLLLLLLB\n" +
    	   					"BLLLBBLLLB\n" +
    	   					"BLLBBBBBLB\n" +
    	   					"BLLLLBBBLB\n" +
    	   					"BLLBLLLLLB\n" +
    	   					"BBBBBBBBBB" );		
		   place = mapa.getMapPiece();
//		   System.out.println(mapa.getPieces().length);
		   int[][]pieces = mapa.pieces;
		   juego = new RobotAndMap(pieces,1,1);
	   }
	   catch(UnmapableException e){System.out.println("Error in board map");}
   }
   
   public void paint(Graphics g) {

	   if (engine.getState() == engine.WELCOME) {
		   g.drawImage(welcomeImage, width/2, height/2, Graphics.HCENTER | Graphics.VCENTER );
	   }
	   else if (engine.getState() == engine.PLAY) {
		   for(int i=0; i< place.length; i++){
			   for (int j=0; j< place[0].length; j++){
	               int tipo = place[i][j].getType();
	               paintMapPiece(g,tipo,i,j);
	            }
	        }
		   int x = juego.getX();
		   int y = juego.getY();
		   paintRobot(g,x,y);
	   }   
   }
   
   public void paintMapPiece(Graphics g,int tipo,int x, int y) {
	   int resto = width-200; 
	   int resto2 = height-160;
	   int coordx = (x*20)+(resto/2);
	   int coordy = (y*20)+(resto2/2);
	   if (tipo==MapPiece.wall) {
		   g.drawImage(wallImage, coordx, coordy, Graphics.TOP | Graphics.LEFT);
	   }
	   else if (tipo==MapPiece.land) {
		   g.drawImage(landImage, coordx, coordy, Graphics.TOP | Graphics.LEFT);
	   }
	   else if (tipo==MapPiece.water) {
		   g.drawImage(waterImage, coordx, coordy, Graphics.TOP | Graphics.LEFT);
	   }
	   else if (tipo==MapPiece.plant) {
		   g.drawImage(plantImage, coordx, coordy, Graphics.TOP | Graphics.LEFT);
	   }
   }
   
   public void paintRobot(Graphics g,int x, int y) {
	   int resto = width-200; 
	   int resto2 = height-160;
	   int coordx = (x*20)+(resto/2);
	   int coordy = (y*20)+(resto2/2);
	   g.drawImage(robotImage, coordx, coordy, Graphics.TOP | Graphics.LEFT);
   }
   public void setEngine(Engine engine) {
       this.engine = engine;
   }
   public void loadMap(String cadena) {
	   try{
		   mapa = new Map (cadena);	
		   place = mapa.getMapPiece();
		   juego = new RobotAndMap(mapa.pieces,1,1);  	
    	   repaint();
	   }
	   catch(UnmapableException e){System.out.println(e.getMessage());}
   }
   
   public RobotAndMap getJuego(){
	   return juego;
   }
   
   public void setJuego(RobotAndMap j){
	   juego = j;
   }
   
   public void clearScreen(){
	   
   }
}

